<?php
/**
 * EY Web Crawling : Step 1 : Employee List
 */

use Custom\Employee;
use Fox\Common\Localization\SubRegion;
use Fox\Common\Localization\City;
use Fox\Common\Localization\City\Street;
use Fox\Html\CheckBox;
use Fox\Html\Input;
use Fox\Html\Screen;

set_time_limit(Xview::TIMEOUT_VERY_LONG);

$screen = new Screen("extract","extract");
$block = new Block("b","Extract Source Files");
$block->add(new TextArea("comments",false));
$block->add(new CheckBox("commit"));
//$block->add(new Input("dbcnt",true,1));
$screen->add($block);

if($screen->posted())
{
	$inputs = $screen->get();
	if(!$screen->errors())
	{
		// Load Files 
		for($i = 'B';$i<='Z';$i++)
		{
			// Open File
			$ficnm = _TEMP_."EY/".$i.".htm";
			// Extract Content 
			$html = file_get_html($ficnm);
			foreach($html->find(".xRow") as $resBlock)
			{
				
				
				$links = $resBlock->find("a");
				// Extract Ressource Id
				$fields['reference'] = preg_replace("/\?Edit\&Lang\=FR/","",substr($links[0]->href,50));
				$fullname = $links[0]->plaintext;
				
				try {
					$employee = new Employee();
					$employee->setFields($fields);
					if($inputs['commit'])
					{
						// Save in the DB
						$employee->create();
					}
					push_msg("I","Employee {$fields['reference']} has been created ");
				}
				catch(Fox\Object\Exception\Save $e)
				{
					push_msg("E",$e->getMessage());
				}
			}
		}
	}
}
echo $screen->display();
//https://emeadmz02.ey.com/marketing/fralumni.nsf/0/2210F5887B67476AC12579AE005BC25F?Edit&Lang=FR